#include "Winsock2.h"
#include "basecommand.h"

//------------------------------------------------------------------------------------
CBaseCommand::CBaseCommand( unsigned int nCommandID ) : 
m_nCommandID(nCommandID),
m_ulSocketID(0),
m_nVersion(PROTOCOL_VERSION)
{
	m_nSequence = 0;
}

CBaseCommand::~CBaseCommand(void)
{
}

unsigned int CBaseCommand::getID( const char* pBuffer )
{
	//Len + cmd + seq
	return ntohl( *(unsigned int *)(pBuffer+sizeof(int)) );
}

int CBaseCommand::Create( char* pBuffer, int& nLen )
{
	int nRet = 0;

	*(unsigned int*)(pBuffer + nRet) = htonl(GetCommandLength());
	nRet += sizeof(m_nCommandLength);
	*(unsigned int*)(pBuffer + nRet) = htonl(m_nCommandID);
	nRet += sizeof(m_nCommandID);
	*(unsigned int*)(pBuffer + nRet) = htonl(m_nSequence);
	nRet += sizeof(m_nSequence);
// 	*(unsigned int*)(pBuffer + nRet) = m_nVersion;
// 	nRet += sizeof(m_nVersion);

	nLen += nRet;
	return nRet;
}

int CBaseCommand::Parse( const char* pBuffer, int nLen )
{
	int nRet = 0;

	m_nCommandLength = ntohl(*(unsigned int*)(pBuffer + nRet));
	nRet += sizeof(m_nCommandLength);
	m_nCommandID = ntohl(*(unsigned int*)(pBuffer + nRet));
	nRet += sizeof(m_nCommandID);
	m_nSequence = ntohl(*(unsigned int*)(pBuffer + nRet));
	nRet += sizeof(m_nSequence);
// 	m_nVersion = *(unsigned int*)(pBuffer + nRet);
// 	nRet += sizeof(m_nVersion);

	nLen -= nRet;
	return nRet;
}

unsigned int CBaseCommand::ID()
{
	return m_nCommandID;
}

unsigned int CBaseCommand::GetSequence()
{
	return m_nSequence;
}

int CBaseCommand::GetCommandLength()
{
	return sizeof(m_nCommandLength) + sizeof(m_nCommandID) + sizeof(m_nSequence);
}
//------------------------------------------------------------------------------------
